package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.project.common.utils.RestUtil;
import com.company.project.pojo.Rest;
import com.company.project.pojo.ServiceCode;
import com.company.project.pojo.contradiction.Contradiction;
import com.company.project.pojo.contradiction.ContradictionDTO;
import com.company.project.pojo.contradiction.ContradictionStatisticsVO;
import com.company.project.pojo.role.Role;
import com.company.project.service.IContradictionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Api(value = "统计",tags = "统计")
@Controller
@RequestMapping("statistics")
public class StatisticsController {

    @Autowired
    private IContradictionService iContradictionService;
    @Autowired
    HttpServletRequest request;

    @ApiOperation(value = "矛盾资料统计")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", value = "单位类型", name = "institutionType", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", value = "单位id", name = "institutionId", dataType = "int"),
            @ApiImplicitParam(paramType="query", value = "村庄id", name = "villageId", dataType = "int")
    })
    @RequestMapping(value = "getContradictionStatistics", method = RequestMethod.GET)
    @ResponseBody
    public Rest getContradictionStatistics(Integer institutionType, Integer institutionId, Integer villageId, String startTime, String endTime){
        List<ContradictionDTO> contradictionDTOList = new ArrayList<>();

        if(institutionType.equals(1)){  // 职能部门
            contradictionDTOList = iContradictionService.getDepartmentsStatistics(institutionId, startTime, endTime);
        }else if(institutionType.equals(2)){    // 管理区
            if(institutionId == null){  // 管理区
                contradictionDTOList = iContradictionService.getManagementzoneStatistics(startTime, endTime);
            }else { // 村庄
                contradictionDTOList = iContradictionService.getVillageStatistics(institutionId, villageId, startTime, endTime);
            }
        }

        List<ContradictionStatisticsVO> contradictionStatisticsVOList = new ArrayList<>();
        for (ContradictionDTO contradictionDTO: contradictionDTOList) {
            ContradictionStatisticsVO contradictionStatisticsVO = new ContradictionStatisticsVO();
            BeanUtils.copyProperties(contradictionDTO, contradictionStatisticsVO);

            contradictionStatisticsVOList.add(contradictionStatisticsVO);
        }

        return RestUtil.rest(ServiceCode.CODE_200, "成功" , contradictionStatisticsVOList);
    }

}
